package com.lanou.dllo.findfood.adpter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.dllo.findfood.R;
import com.lanou.dllo.findfood.entity.MenuResult;
import com.lanou.dllo.findfood.util.Menuimage;
import com.lanou.dllo.findfood.volley.VolleySington;

import java.util.List;

/**
 * Created by dllo on 15/10/23.
 */
public class LoseRecylerAdpter extends RecyclerView.Adapter<LoseRecylerAdpter.recylerAdpter> {


    private List<MenuResult> result;
    private Menuimage mlistener;
    private VolleySington volleySington;

    public void addData(List<MenuResult> results) {

        result = results;
    }
    public  void setImageListener(Menuimage listener){

        mlistener = listener;
    }
    public LoseRecylerAdpter() {
        volleySington = VolleySington.getInstance();
    }

    @Override
    public recylerAdpter onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.lose_recy_item, null);
        return new recylerAdpter(view);
    }

    @Override
    public void onBindViewHolder(recylerAdpter holder, int position) {
        if (result != null && result.size() > 0) {
            MenuResult re = result.get(position);
            holder.titlel.setText(re.getName());
            holder.keylose.setText(re.getKeywords());
            if (re.getImg() != null && re.getImg().length() > 0) {
                String imgUrl = re.getImg();
                String url = " http://tnfs.tngou.net/image/" + imgUrl;
                getBitmap(url, holder.titleloseIv);
            }


        }
        holder.position = position;


    }

    class recylerAdpter extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView discreptlose, keylose, titlel, discreptionlose;
        private NetworkImageView titleloseIv;
        private int position;

        public recylerAdpter(View itemView) {
            super(itemView);
            discreptionlose = (TextView) itemView.findViewById(R.id.discreptionlose);//详情
            discreptionlose.setOnClickListener(this);
            discreptlose = (TextView) itemView.findViewById(R.id.discreptlose);

            keylose = (TextView) itemView.findViewById(R.id.keylose);
            titlel = (TextView) itemView.findViewById(R.id.titlel);
            titleloseIv = (NetworkImageView) itemView.findViewById(R.id.titleloseIv);
            titleloseIv.setOnClickListener(this);

        }

        private int count = 0;//0代表设置内容

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.discreptionlose:
                    if (count == 0) {
                        if (result != null && result.size() > 0) {
                            MenuResult resu = result.get(position);
                            String discreption = resu.getDescription();
                            discreptlose.setText(discreption);
                        }
                        count = 1;

                    } else if (count == 1) {
                        discreptlose.setText(null);
                        count = 0;
                    } else {
                        discreptlose.setText(null);
                    }


                    break;
                case R.id.titleloseIv:
                    if (result != null && result.size() > 0) {
                        MenuResult resu = result.get(position);
                        if (mlistener != null){
                            mlistener.MenuimageOnclick(resu);
                        }
                    }

                    break;
                default:
                    break;
            }
        }
    }

    private void getBitmap(String url, final NetworkImageView networkImageView) {
        ImageLoader imageLoader = volleySington.getImageLoader();
        networkImageView.setDefaultImageResId(R.mipmap.ic_launcher);
        networkImageView.setErrorImageResId(R.mipmap.ic_launcher);
        networkImageView.setImageUrl(url, imageLoader);


    }

    @Override
    public int getItemCount() {
        return result != null && result.size() > 0 ? result.size() : 0;
    }


}
